package com.myself.realm;

import com.myself.entity.Permission;
import com.myself.entity.Role;
import com.myself.entity.User;
import com.myself.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Description:
 *
 * @Author: Liuzemin
 * @Date: 2020/7/13 13:14
 */
public class UserRealm extends AuthorizingRealm {
    @Autowired
    UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("权限验证执行");
        String username = (String) principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        Set<String> roleSet = new HashSet<>();
        List<Role> roles = userService.findRole(username);
        for (Role role : roles) {
            roleSet.add(role.getRole());
        }
        authorizationInfo.setRoles(roleSet);

        Set<String> permissionSet = new HashSet<>();
        List<Permission> permissions = userService.findPermission(username);
        for (Permission permission : permissions) {
            permissionSet.add(permission.getPermission());
        }
        authorizationInfo.setStringPermissions(permissionSet);

        return authorizationInfo;
    }



    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("身份验证执行");
        String username = (String) authenticationToken.getPrincipal();
        User user = userService.findByName(username);
        if (user == null){
            throw new UnknownAccountException("没有该账号");
        }

        if (user.getLocked().equals(Boolean.TRUE)){
            throw new LockedAccountException("账号被锁定");
        }

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                user.getUsername(),
                user.getPassword(),
                ByteSource.Util.bytes(user.getCredentialsSalt()),
                getName());

        return authenticationInfo;
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }
}
